package com.jasmine.bjstkserver.server.dict;

import cn.hutool.core.util.StrUtil;
import com.jasmine.auth.util.AuthUtil;
import com.jasmine.bjstkserver.core.config.annotation.AuthIgnoreLimiter;
import com.jasmine.core.dto.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author jasmineXz
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @GetMapping
    public R findList(@ModelAttribute DictEntity dict){
        dict.setUserId(AuthUtil.getUserId());
        return R.ok(dictService.findList(dict));
    }

    @PutMapping("updates")
    public R update(@RequestBody Map<Integer,String> map) {
        map.forEach((k,v) -> {
            DictEntity dictEntity = new DictEntity();
            dictEntity.setUserId(AuthUtil.getUserId());
            dictEntity.setDictId(Integer.parseInt(StrUtil.sub(k.toString(),0,4)));
            dictEntity.setDictCid(k);
            dictEntity.setDictValue(v);
            dictService.updateDict(dictEntity);
        });
        return R.ok();
    }

    /**
     * 刷新字典,无需权限
     */
    @AuthIgnoreLimiter
    @GetMapping("/refresh")
    public R refresh(){
        dictService.refreshDictAll();
        return R.ok();
    }
}
